ta-lib¶

In [2]:
#pip install pandas_ta
In [51]:
import pandas as pd
import numpy as np
import pandas_ta as ta

import math
import yfinance as yf
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

from IPython.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))
In [30]:
df_nvda = yf.download('NVDA', start='2022-04-01',
               end='2023-01-01')
df_nvda
[*********************100%***********************]  1 of 1 completed
Out[30]:
Open High Low Close Adj Close Volume
Date
2022-04-01 27.375000 27.496000 26.267000 26.712000 26.676310 517235000
2022-04-04 26.728001 27.558001 26.613001 27.360001 27.323444 397120000
2022-04-05 27.254000 27.319000 25.820000 25.931000 25.896355 436615000
2022-04-06 24.934000 25.299999 24.003000 24.407000 24.374392 703833000
2022-04-07 24.441000 24.722000 23.478001 24.208000 24.175659 557992000
... ... ... ... ... ... ...
2022-12-23 15.196000 15.339000 14.883000 15.206000 15.197294 349326000
2022-12-27 15.074000 15.100000 14.056000 14.121000 14.112915 464902000
2022-12-28 13.927000 14.262000 13.884000 14.036000 14.027966 351066000
2022-12-29 14.402000 14.683000 14.227000 14.603000 14.594640 354923000
2022-12-30 14.334000 14.629000 14.233000 14.614000 14.605634 310490000

189 rows × 6 columns

Stochastic & RSI¶

In [28]:
ta.stochrsi( df_nvda['Close'])
Out[28]:
STOCHRSIk_14_14_3_3 STOCHRSId_14_14_3_3
Date
2022-04-01 NaN NaN
2022-04-04 NaN NaN
2022-04-05 NaN NaN
2022-04-06 NaN NaN
2022-04-07 NaN NaN
... ... ...
2022-12-23 6.355971e+00 6.355971
2022-12-27 -3.789561e-14 4.237314
2022-12-28 -3.789561e-14 2.118657
2022-12-29 5.700989e+00 1.900330
2022-12-30 1.151089e+01 5.737293

189 rows × 2 columns

In [35]:
df_nvda['rsi'] = ta.rsi(df_nvda['Close'], length=14)
df_nvda[['k','d']] = ta.stochrsi( df_nvda['Close'])
df_nvda.tail(3)
Out[35]:
Open High Low Close Adj Close Volume rsi k d
Date
2022-12-28 13.927 14.262 13.884 14.036 14.027966 351066000 35.526350 -3.789561e-14 2.118657
2022-12-29 14.402 14.683 14.227 14.603 14.594640 354923000 40.946105 5.700989e+00 1.900330
2022-12-30 14.334 14.629 14.233 14.614 14.605634 310490000 41.049644 1.151089e+01 5.737293
In [36]:
fig = make_subplots(rows=3, cols=1, shared_xaxes=True, 
                    vertical_spacing=0.01, row_heights=[0.7, 0.15, 0.15])
fig.add_trace(go.Candlestick(x=df_nvda.index,
            open=df_nvda['Open'], high=df_nvda['High'],
            low=df_nvda['Low'], close=df_nvda['Close'],
            showlegend=True, name='Close Price'),row=1,col=1 )
fig.add_trace(go.Scatter(x=df_nvda.index,
                         y=df_nvda['k'], name='Stoch - k',
                         line=dict(color='red', width=1)
                        ), row=2, col=1)
fig.add_trace(go.Scatter(x=df_nvda.index,
                         y=df_nvda['d'], name='Stoch - d',
                         line=dict(color='blue', width=1)
                        ), row=2, col=1)
fig.add_trace(go.Scatter(x=df_nvda.index, y=df_nvda['rsi'], name='RSI',
                    marker=dict(color ='purple' )), row=3, col=1)
fig.update_layout(title="NVDIA Share Price (Close) US$",
                  xaxis_rangeslider_visible=False)
fig.show('notebook')
In [37]:
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                    vertical_spacing=0.01, row_heights=[0.7, 0.3])
fig.add_trace(go.Candlestick(x=df_nvda.index,
            open=df_nvda['Open'], high=df_nvda['High'],
            low=df_nvda['Low'], close=df_nvda['Close'],
            showlegend=True, name='Close Price'),row=1,col=1 )
fig.add_trace(go.Scatter(x=df_nvda.index, y=df_nvda['rsi'], name='RSI',
                    marker=dict(color ='purple' )), row=2, col=1)
fig.update_layout(title="NVDIA Share Price (Close) US$",
                  xaxis_rangeslider_visible=False)
fig.show('notebook')
In [ ]:
 

Bollinger Bands¶

In [40]:
ta.bbands(df_nvda['Close'])
Out[40]:
BBL_5_2.0 BBM_5_2.0 BBU_5_2.0 BBB_5_2.0 BBP_5_2.0
Date
2022-04-01 NaN NaN NaN NaN NaN
2022-04-04 NaN NaN NaN NaN NaN
2022-04-05 NaN NaN NaN NaN NaN
2022-04-06 NaN NaN NaN NaN NaN
2022-04-07 23.237119 25.7236 28.210081 19.332295 0.195232
... ... ... ... ... ...
2022-12-23 14.851543 15.8770 16.902457 12.917516 0.172829
2022-12-27 13.814326 15.4504 17.086473 21.178395 0.093723
2022-12-28 13.228629 15.0406 16.852571 24.094403 0.222788
2022-12-29 13.586832 14.6610 15.735169 14.653414 0.473002
2022-12-30 13.677097 14.5160 15.354903 11.558323 0.558410

189 rows × 5 columns

In [41]:
df_nvda[['lower','middle','upper','bwidth','percent' ]] = ta.bbands(df_nvda['Close'])
In [45]:
fig=go.Figure(data=[go.Candlestick(x=df_nvda.index,
            open=df_nvda['Open'], high=df_nvda['High'],
            low=df_nvda['Low'], close=df_nvda['Close'],
            showlegend=True, name='Close Price')] )

# BB middle
fig.add_trace(go.Scatter(x = df_nvda.index, y = df_nvda['middle'],
            line_color = 'black', name = 'sma'))

# Upper Bound
fig.add_trace(go.Scatter(x = df_nvda.index, y = df_nvda['upper'],
            line_color = 'gray', line = {'dash': 'dash'},
            name = 'upper band', opacity = 0.5))

# Lower Bound fill in between with parameter 'fill': 'tonexty'
fig.add_trace(go.Scatter(x = df_nvda.index, y = df_nvda['lower'],
            line_color = 'cyan', line = {'dash': 'dash'},
            fill = 'tonexty', name = 'lower band', opacity = 0.1))

fig.update_layout(title="NVDIA Share Price (Close) US$",
                  xaxis_rangeslider_visible=False)

MACD¶

In [48]:
df_nvda[['macd','macdhist','macdsignal']] = ta.macd(df_nvda['Close'], fastperiod=12, slowperiod=26, signalperiod=9)
In [52]:
# Colorize the histogram values
colors = np.where(df_nvda['macdhist'] < 0, '#008000', '#FF00FF')

fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                    vertical_spacing=0.01, row_heights=[0.8, 0.2])
fig.add_trace(go.Candlestick(x=df_nvda.index,
            open=df_nvda['Open'], high=df_nvda['High'],
            low=df_nvda['Low'], close=df_nvda['Close'],
            showlegend=True, name='Close Price'),row=1,col=1 )
# MACD 
fig.append_trace(
    go.Scatter(x=df_nvda.index, y=df_nvda['macd'],
        line=dict(color='#ff9900', width=2), name='macd',
        legendgroup='2',), row=2, col=1)

# MACD Signal
fig.append_trace(
    go.Scatter(x=df_nvda.index, y=df_nvda['macdsignal'],
        line=dict(color='#000000', width=2),
        legendgroup='2', name='signal'), row=2, col=1)

# histogram
fig.append_trace(
    go.Bar(x=df_nvda.index, y=df_nvda['macdhist'], name='histogram',
        marker_color=colors,), row=2, col=1)

fig.update_layout(title="NVDIA Share Price (Close) US$",
                  xaxis_rangeslider_visible=False)
fig.show()

ADOSC Accumulation/Distribution Oscillator¶

In [53]:
ta.adosc(df_nvda['High'], df_nvda['Low'],df_nvda['Close'],
                    df_nvda['Volume'],fastperiod=3, slowperiod=10)
Out[53]:
Date
2022-04-01             NaN
2022-04-04             NaN
2022-04-05             NaN
2022-04-06             NaN
2022-04-07             NaN
                  ...     
2022-12-23   -7.345105e+07
2022-12-27   -1.753269e+08
2022-12-28   -2.229321e+08
2022-12-29   -1.488350e+08
2022-12-30   -1.368375e+07
Name: ADOSC_3_10, Length: 189, dtype: float64
In [54]:
df_nvda['adosc'] = ta.adosc(df_nvda['High'], df_nvda['Low'],df_nvda['Close'],
                    df_nvda['Volume'],fastperiod=3, slowperiod=10)
In [55]:
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                    vertical_spacing=0.01, row_heights=[0.7, 0.3])
fig.add_trace(go.Candlestick(x=df_nvda.index,
            open=df_nvda['Open'], high=df_nvda['High'],
            low=df_nvda['Low'], close=df_nvda['Close'],
            showlegend=True, name='Close Price'),row=1,col=1 )
fig.add_trace(go.Scatter(x=df_nvda.index, y=df_nvda['adosc'], name='ADOSC',
                    marker=dict(color ='purple' )), row=2, col=1)
fig.update_layout(title="NVDIA Share Price (Close) US$",
                  xaxis_rangeslider_visible=False)
fig.show()

ATR average true range¶

In [57]:
df_nvda['atr'] = ta.atr(df_nvda['High'], df_nvda['Low'],df_nvda['Close'], timeperiod=14)
df_nvda['tr'] = ta.true_range(df_nvda['High'], df_nvda['Low'],df_nvda['Close'])
In [58]:
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, 
                    vertical_spacing=0.01, row_heights=[0.7, 0.3])
fig.add_trace(go.Candlestick(x=df_nvda.index,
            open=df_nvda['Open'], high=df_nvda['High'],
            low=df_nvda['Low'], close=df_nvda['Close'],
            showlegend=True, name='Close Price'),row=1,col=1 )
fig.add_trace(go.Scatter(x=df_nvda.index, y=df_nvda['atr'], name='ATR',
                    marker=dict(color ='orange' )), row=2, col=1)
fig.add_trace(go.Scatter(x=df_nvda.index, y=df_nvda['tr'], name='TR',
                    marker=dict(color ='purple' )), row=2, col=1)
fig.update_layout(title="NVDIA Share Price (Close) US$",
                  xaxis_rangeslider_visible=False)
fig.show()
In [ ]: